package renguanyu.app.tools.model.anjuke;

import java.sql.SQLException;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import renguanyu.app.tools.util.DbUtil;

/**
 * 安居客二手房列表元素
 */
public class AnjukeListElement {
	
	private String code = "";
	private String title = "";
	private String anxuan = "N";
	private String structure = "";
	private String area = "";
	private String toward = "";
	private String floor = "";
	private String createYear = "";
	private String communityName = "";
	private String communityAddress = "";
	private String totalPrice = "";
	private String avgPrice = "";
	private String createAt = DateUtil.getCurrentDate();

	public String getAnxuan() {
		return anxuan;
	}

	public String getArea() {
		return area;
	}

	public String getAvgPrice() {
		return avgPrice;
	}

	public String getCode() {
		return code;
	}

	public String getCommunityAddress() {
		return communityAddress;
	}

	public String getCommunityName() {
		return communityName;
	}

	public String getCreateYear() {
		return createYear;
	}

	public String getFloor() {
		return floor;
	}

	public String getStructure() {
		return structure;
	}

	public String getTitle() {
		return title;
	}

	public String getTotalPrice() {
		return totalPrice;
	}

	public String getToward() {
		return toward;
	}

	public void save2Mysql() throws SQLException {
		StringBuilder sql = new StringBuilder();
		sql.append(
				"REPLACE INTO anjuke_second_house (code, name, anxuan, `structure`, area, toward, floor, create_year, community_name, community_address, total_price, avg_price, created_at) ");
		sql.append("VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?); ");
		DbUtil.getQueryRunner().update(sql.toString(), this.code, this.title, this.anxuan, this.structure, this.area,
				this.toward, this.floor, this.createYear, this.communityName, this.communityAddress, this.totalPrice,
				this.avgPrice, this.createAt);
	}

	public void setAnxuan(Element element) {
		Element anxuan = element.selectXpath("//img[@class='property-content-title-anxuan']").first();
		if (anxuan == null)
			return;
		setAnxuan("Y");
	}

	public void setAnxuan(String anxuan) {
		this.anxuan = anxuan;
	}

	public void setArea(Element element) {
		Element area = element.selectXpath("//p[@class='property-content-info-text'][1]").first();
		if (area == null)
			return;
		this.setArea(area.html().replace("㎡", ""));
	}

	public void setArea(String area) {
		this.area = area;
	}

	public void setAvgPrice(Element element) {
		Element avgPrice = element.selectXpath("//p[@class='property-price-average']").first();
		if (avgPrice == null)
			return;
		setAvgPrice(avgPrice.html().replace("元/㎡", ""));
	}

	public void setAvgPrice(String avgPrice) {
		this.avgPrice = avgPrice;
	}

	public void setCode(Element element) {
		Element code = element.selectXpath("//a[@class='property-ex']").first();
		String href = code.attr("href");
		setCode(href.substring(0, href.indexOf("?")).substring(href.lastIndexOf("/") + 1));
	}

	public void setCode(String code) {
		this.code = code;
	}

	public void setCommunityAddress(Element element) {
		Element communityAddress = element.selectXpath("//p[@class='property-content-info-comm-address']").first();
		StringBuilder htmls = new StringBuilder();
		Elements children = communityAddress.children();
		for (Element e : children) {
			String html = e.html();
			htmls.append(html);
		}
		setCommunityAddress(htmls.toString());
	}

	public void setCommunityAddress(String communityAddress) {
		this.communityAddress = communityAddress;
	}

	public void setCommunityName(Element element) {
		Element communityName = element.selectXpath("//p[@class='property-content-info-comm-name']").first();
		if (communityName == null)
			return;
		setCommunityName(communityName.html());
	}

	public void setCommunityName(String communityName) {
		this.communityName = communityName;
	}

	public void setCreateYear(Element element) {
		Element createYear = element.selectXpath("//p[@class='property-content-info-text'][4]").first();
		if (createYear == null)
			return;
		setCreateYear(createYear.html().replace("年建造", ""));
	}

	public void setCreateYear(String createYear) {
		this.createYear = createYear;
	}

	public void setFloor(Element element) {
		Element floor = element.selectXpath("//p[@class='property-content-info-text'][3]").first();
		if (floor == null)
			return;
		setFloor(floor.html());
	}

	public void setFloor(String floor) {
		this.floor = floor;
	}

	public void setStructure(Element element) {
		Element structure = element
				.selectXpath("//p[@class='property-content-info-text property-content-info-attribute']").first();
		StringBuilder htmls = new StringBuilder();
		Elements children = structure.children();
		for (Element e : children) {
			String html = e.html();
			htmls.append(html);
		}
		setStructure(htmls.toString());
	}

	public void setStructure(String structure) {
		this.structure = structure;
	}

	public void setTitle(Element element) {
		Element title = element.selectXpath("//h3[@class='property-content-title-name']").first();
		this.title = title.text();
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setTotalPrice(Element element) {
		Element totalPrice = element.selectXpath("//span[@class='property-price-total-num']").first();
		if (totalPrice == null)
			return;
		setTotalPrice(totalPrice.html());
	}

	public void setTotalPrice(String totalPrice) {
		this.totalPrice = totalPrice;
	}

	public void setToward(Element element) {
		Element toward = element.selectXpath("//p[@class='property-content-info-text'][2]").first();
		if (toward == null)
			return;
		setToward(toward.html());
	}

	public void setToward(String toward) {
		this.toward = toward;
	}

	@Override
	public String toString() {
		return "AnjukeListElement{" + "code='" + code + '\'' + ", title='" + title + '\'' + ", anxuan='" + anxuan + '\''
				+ ", structure='" + structure + '\'' + ", area='" + area + '\'' + ", toward='" + toward + '\''
				+ ", floor='" + floor + '\'' + ", createYear='" + createYear + '\'' + ", communityName='"
				+ communityName + '\'' + ", communityAddress='" + communityAddress + '\'' + ", totalPrice='"
				+ totalPrice + '\'' + ", avgPrice='" + avgPrice + '\'' + ", createAt='" + createAt + '\'' + '}';
	}
}
